﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NExplorer
{
	internal class ColumnProviderManager : IColumnProviderManager
	{
		public event EventHandler<EventArgs<IColumnProvider>> ColumnProviderAdded;
		public event EventHandler<EventArgs<IColumnProvider>> ColumnProviderRemoved;

		private readonly List<ColumnProviderInfo> _providerInfos = new List<ColumnProviderInfo>();
		private readonly int _startIndex = 4;

		public IList<ColumnProviderInfo> ProviderInfos
		{
			get { return _providerInfos;  }
		}

		public void AddColumnProvider(IColumnProvider columnProvider)
		{
			_providerInfos.Add(new ColumnProviderInfo(columnProvider, _startIndex + _providerInfos.Count));

			if (ColumnProviderAdded != null)
			{
				ColumnProviderAdded(this, new EventArgs<IColumnProvider>(columnProvider));
			}
		}

		public void RemoveColumnProvider(IColumnProvider columnProvider)
		{
			// TODO: Adjust remaining indexes when provider is removed.
			ColumnProviderInfo foundInfo = _providerInfos.Find(info => info.ColumnProvider == columnProvider);
			
			if (foundInfo != null)
			{
				_providerInfos.Remove(foundInfo);

				if (ColumnProviderRemoved != null)
				{
					ColumnProviderRemoved(this, new EventArgs<IColumnProvider>(columnProvider));
				}
			}
		}
	}
}
